<template>
  <div>
    <!-- Breadcrumb 面包屑 -->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>常数管理</el-breadcrumb-item>
      <el-breadcrumb-item>常数列表</el-breadcrumb-item>
    </el-breadcrumb>
    <!-- 搜索 -->
    <el-row :gutter="20">
      <el-col :span="12">
        <el-input placeholder="常数查询" v-model="queryInfo.query">
          <el-button
            slot="append"
            icon="el-icon-search"
            @click="getConList"
          ></el-button>
        </el-input>
      </el-col>
      <el-col :span="4">
        <el-button type="primary" @click="depDialogVisible = true"
          >新增常数</el-button
        >
      </el-col>
      <el-col :span="4">
        <el-button type="success" @click="editDialogVisible = true"
          >查询常数</el-button
        >
      </el-col>
      <el-col :span="4">
        <el-button type="danger" @click="removeBatch(sels)">批量删除</el-button>
      </el-col>
    </el-row>

    <!-- card展示 -->
    <el-card>
     
      <el-table @selection-change="selsChange"   :data="conList" border stripe>
        <!-- 为表格添加一个编号（索引） -->
        <el-table-column type="selection" width="65"></el-table-column>
        <el-table-column type="index" label="编号"> </el-table-column>
        <el-table-column prop="constanttype.constanttypename" label="所属常数类别ID"></el-table-column>
        <el-table-column prop="constantcode" label="常数项编码"> </el-table-column>
        <el-table-column prop="constantname" label="常数项名称"> </el-table-column>
        <el-table-column width="200px" label="操作">
          <template slot-scope="scope">
            <!-- 修改按钮 -->
            <el-button
              type="primary"
              icon="el-icon-edit"
              size="mini"
              @click="showEditDialog(scope.row.id)"
            ></el-button>
            <!-- 删除按钮 -->
            <el-tooltip
              effect="dark"
              content="删除信息"
              placement="top"
              :enterable="false"
            >
              <el-button
                type="danger"
                icon="el-icon-delete"
                size="mini"
                @click="removeDepById(scope.row.id)"
              ></el-button>
            </el-tooltip>
          </template>
        </el-table-column>
      </el-table>
         <!-- Pagination分页 -->
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="queryInfo.pageNum"
        :page-sizes="[1, 2, 5, 10]"
        :page-size="queryInfo.pageSize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
      >
      </el-pagination>
    </el-card>
    <el-dialog title="添加常数" :visible.sync="depDialogVisible" width="50%">
      <!-- 创建一个添加常数表单 -->
      <!-- 常数类别 常数名称 常数编码类-->
      <!-- 写完表单后要添加数据绑定以及检验的规则 -->
      <el-form :model="conForm" :rules="conFormRules" ref="conFormRef">
        <el-form-item prop="constantcode" label="常数项编码 ">
          <el-input
            v-model="conForm.constantcode"
            prefix-icon="iconfont icon-user"
            placeholder="请填写常数项编码"
          ></el-input>
        </el-form-item>
        <el-form-item prop="constantname" label="常数项名称 ">
          <el-input
            v-model="conForm.constantname"
            prefix-icon="iconfont icon-user"
            placeholder="请填写常数项名称 "
          ></el-input>
        </el-form-item>
       <!-- 常数项类型  下拉列表 -->
        <el-form-item prop="constanttypeid" label="常数项类型">
         <el-select v-model="conForm.constanttypeid" placeholder="请选择">
    <el-option
      v-for="item in typeList"
      :key="item.id"
      :label="item.constanttypename"
      :value="item.id">
    </el-option>
  </el-select>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button  @click="depDialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="sureAdd">确 定</el-button>
      </span>
    </el-dialog>

    <!-- 查询用户的对话框 -->
    <el-dialog
      title="查询用户"
      :visible.sync="editDialogVisible"
      width="80%"
    >
      <el-form
:rules="qqFormRules" ref="qqFormRef"

        :model="queryForm"
        label-width="100px"
      >
        <el-form-item label="常数项编码 ">
          <el-input v-model="queryForm.constantcode" prop="constantcode" ></el-input>
        </el-form-item>
        <el-form-item label="常数项名称" prop="constantname">
          <el-input v-model="queryForm.constantname "></el-input>
        </el-form-item>
         <!-- 常数项类型  下拉列表 -->
        <el-form-item prop="constanttypeid" label="常数项类型">
         <el-select v-model="queryForm.constanttypeid" placeholder="请选择">
    <el-option
      v-for="item in typeList"
      :key="item.id"
      :label="item.constanttypename"
      :value="item.id">
    </el-option>
  </el-select>
        </el-form-item>
        
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click=" editDialogVisible=false">取 消</el-button>
        <el-button type="primary" @click="queryUser">查询</el-button>
      </span>
    </el-dialog>

     <el-dialog title="修改常数项" :visible.sync="updateDialogVisible" width="50%">
      <!-- 创建一个表单 -->
      <!-- 修改-->
      <!-- 写完表单后要修改数据绑定以及检验的规则 -->
      <el-form :model="updateForm" :rules="updateFormRules" ref="updateFormRef">
        <el-form-item prop="constantcode" label="常数项编码">
          <el-input
            v-model="updateForm.constantcode"
            prefix-icon="iconfont icon-user"
            placeholder="请填常数项编码"
          ></el-input>
        </el-form-item>
        <el-form-item prop="constantname" label="常数项名称">
          <el-input
            v-model="updateForm.constantname"
            prefix-icon="iconfont icon-user"
            placeholder="请输常数项名称"
          ></el-input>
        </el-form-item>
          <!-- 常数项类型  下拉列表 -->
        <el-form-item prop="constanttypeid" label="常数项类型">
         <el-select v-model="updateForm.constanttypeid" placeholder="请选择">
    <el-option
      v-for="item in typeList"
      :key="item.id"
      :label="item.constanttypename"
      :value="item.id">
    </el-option>
  </el-select>
        </el-form-item>
       
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="updateDialogVisible=false">取 消</el-button>
        <el-button type="primary" @click="sureUpdate">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
export default {
    data(){
        return{
//查询所有科室列表信息
          total:0,
          queryInfo:{
          pageNum:1,
          pageSize:2
        },
//科室列表信息
        conList:[],
         sels:[],
        typeList:[],
//科室添加Dilog框默认为关闭
         depDialogVisible:false,
//科室查询Dilog框默认关闭
        editDialogVisible:false,
//修改查询Dilog框默认关闭        
        updateDialogVisible:false,

//添加信息表单
conForm:{
  constantcode:"",
  constantname:"",
  constanttypeid:null,
},
//查询表单信息
queryForm:{
  constantcode:"",
  constantname:"",
  constanttypeid:null,
},
//修改表单信息
updateForm:{
  id:null,
   constantcode:"",
  constantname:"",
  constanttypeid:null,
},
 conFormRules:{
        constantcode:[
           { required: true, message: "请输入常数项编码", trigger: "blur" },
          ],
        constantname: [
          { required: true, message: "请输入常数项名称", trigger: "blur" },
         
        ],
      },
      //修改信息表单验证
      updateFormRules:{
       constantcode:[
           { required: true, message: "请输入常数项编码", trigger: "blur" },
          ],
        constantname: [
          { required: true, message: "请输入常数项名称", trigger: "blur" },
         
        ],
      },

       //修改信息表单验证
      qqFormRules:{
       deptcode:[
           { required: true, message: "不能为空", trigger: "blur" },
          ],
        deptname: [
          { required: true, message: "请输入密码", trigger: "blur" },
         
        ],
          deptcategoryid:[
            { required: true, message: "不能为空", trigger: "blur" },
         
         ],
        depttype:[  { required: true, message: "不能为空", trigger: "blur" }]
      },
        }
       
    },
     created() {
    this.getConList();
    this.getTypeList();
  },
    methods:{
        //查询所有科室列表
       async getConList(){
 const {data:res}=await this.$http.get(`constantitem/queryAll`, { params: this.queryInfo});
      // 如果状态异常
      if (res.meta.status !== 200) {
        return this.$message.error("获取所有常数列表失败");
      }
     // 如果状态正常，获取用户数据
         this.conList=res.list;
       this.total = res.total;
    console.log(res);
        },
     async   getTypeList(){
const {data:res}=await this.$http.get(`constanttype/queryConstantName`);
      // 如果状态异常
      if (res.meta.status !== 200) {
        return this.$message.error("获取医生职称信息失败");
      }
    this.conList=res.list;
     // 如果状态正常，获取用户数据
      this.typeList = res.list;
    console.log(res);
        },

        //添加信息 确定
   async sureAdd(){
     const {data:res}= await this.$http.post(`constantitem/insertCon`, 
       this.conForm
      )
        // 如果状态异常
      if (res.meta.status !== 200) {
        return this.$message.error("添加用户信息失败");
      }
     this.$refs.conFormRef.resetFields();
       this.depDialogVisible=false;
        this.getConList();
    },

//分页按钮功能
         handleSizeChange(size) {
      // console.log(size)
      this.queryInfo.pageSize = size;
      //重新查询科室信息
     this.getConList();
    },
    handleCurrentChange(page) {
      // console.log(page)
      this.queryInfo.pageNum = page;
      //重新查询科室信息
      this.getConList();
    },
//根据id删除科室信息
   //删除用户信息
    async removeDepById(row){
     const{data:res}=await this.$http.get(`constantitem/deleteConById`, {
        params: {id:row}
      })
       // 如果状态异常
      if (res.meta.status !== 200) {
        return this.$message.error("删除用户信息失败");
      }
      this.getTypeList();
     this.getConList();
    },

//根据条件查询用户信息
    async queryUser(){
     const { data: res } = await this.$http.post("constantitem/queryCon",this.queryForm);
     //如果状态异常
      // 如果状态异常
      if (res.meta.status !== 200) {
        return this.$message.error("获取用户信息失败");
      }
       // 如果状态正常，获取用户数据
      this.conList = res.department;
       this.$refs.qqFormRef.resetFields();
      console.log(res);
       this.editDialogVisible=false
    },

      //批量删除 向后台发送请求
  async removeBatch(rows){
  // 弹框询问用户是否删除数据
      const confirmResult = await this.$confirm(
        "此操作将永久删除该用户, 是否继续?",
        "提示",
        {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }
      ).catch(err => err);

      // 如果用户确认删除，则返回值为字符串 confirm
      // 如果用户取消了删除，则返回值为字符串 cancel
      // console.log(confirmResult)

      if (confirmResult !== "confirm") {
        return this.$message.info("已取消删除");
      }

    var ids = [];
    rows.forEach(element=>{
      ids.push(element.id)
    })

    const{data:res} =await this.$http.post(`constantitem/deleteByChoose`,{info:ids});
     // 如果状态异常
      if (res.meta.status !== 200) {
        return this.$message.error("删除用户信息失败");
      }
         this.getTypeList();
     this.getConList();
 },
      //批量删除 复选框 选中id放进数组
    selsChange(sels){
     this.sels=sels
    },

//修改对话框
   async showEditDialog(row){
        this.updateDialogVisible=true
       //根据id获取用户信息
      const{data:res}=await this.$http.get(`constantitem/selectConById`,{  params: {id:row}})
        // 如果状态异常
      if (res.meta.status !== 200) {
        return this.$message.error("根据id用户信息失败");}
        console.log(res)
             this.updateForm.constantcode=res.constantitem.constantcode
             this.updateForm.constantname=res.constantitem.constantname
             this.updateForm.constanttypeid=res.constantitem.constanttypeid
              this.updateForm.id=row
       },
//确定修改科室信息
async sureUpdate(){
     const{data:res}=await this.$http.post(`constantitem/updateUser`,  this.updateForm);
       if (res.meta.status !== 200) {
        return this.$message.error("修改用户信息失败");
      }
       this.$refs.updateFormRef.resetFields();
           this.getConList();
    this.getTypeList();
          this.updateDialogVisible=false;
       }

    }
}
</script>

<style lang="less" scoped></style>
